package com.bw.smartpark.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class DjsView extends View {
    // 绘制参数
    private Paint backgroundPaint;
    private Paint progressPaint;
    private Paint textPaint;
    private RectF progressRect = new RectF();

    // 尺寸参数
    private float centerX;
    private float centerY;
    private float radius;
    private float strokeWidth = 5f; // 圆环宽度

    // 倒计时参数
    private long totalTime = 60000; // 总时间（毫秒）
    private long remainingTime = 60000; // 剩余时间（毫秒）
    private int progress = 100; // 进度百分比

    public DjsView(Context context) {
        super(context);
        init();
    }

    public DjsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // 背景圆环画笔（白色）
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setStyle(Paint.Style.STROKE);
        backgroundPaint.setStrokeWidth(strokeWidth);
        backgroundPaint.setColor(Color.WHITE);

        // 进度条画笔（蓝色）
        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(strokeWidth);
        progressPaint.setColor(Color.parseColor("#FF4285F4")); // 蓝色
        progressPaint.setStrokeCap(Paint.Cap.ROUND); // 圆角端点

        // 文字画笔
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 计算中心点和半径
        centerX = w / 2f;
        centerY = h / 2f;
        radius = Math.min(w, h) / 2f - strokeWidth;

        // 设置进度条矩形区域
        progressRect.set(
                centerX - radius,
                centerY - radius,
                centerX + radius,
                centerY + radius
        );

        // 根据视图大小调整文字大小
        textPaint.setTextSize(radius * 0.5f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 1. 绘制背景圆环（白色）
        canvas.drawCircle(centerX, centerY, radius, backgroundPaint);

        // 2. 绘制进度条（从顶部开始）
        float sweepAngle = 360 * progress / 100f;
        canvas.drawArc(progressRect, -90, sweepAngle, false, progressPaint);

        // 3. 绘制倒计时文字
        String timeText = formatTime(remainingTime);
        canvas.drawText(timeText, centerX, centerY + textPaint.getTextSize()/3, textPaint);
    }

    // 设置倒计时时间（毫秒）
    public void setRemainingTime(long millis) {
        this.remainingTime = millis;
        this.progress = (int) (100 * millis / totalTime);
        invalidate(); // 触发重绘

    }

    // 设置总时间（毫秒）
    public void setTotalTime(long totalTime) {
        this.totalTime = totalTime;
        this.progress = (int) (100 * remainingTime / totalTime);
        invalidate();
    }

    private String formatTime(long millis) {
        int seconds = (int)Math.ceil (millis / 1000.0);
        return seconds+"s";
    }
}
